/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO_BUS;

import java.util.Vector;
import org.w3c.dom.Document;

/**
 *
 * @author MinhNhat
 */
public class MySentence {
    
    public final int Null = -1;
    public final int TABLE = 0;
    public void Print()
    {
        
    }
    public boolean Excute(Document doc, Vector Tables)
    {
        return false;
    }
    
    public Vector GetConditions(String str)
    {
        Vector Conditions= new Vector();
        String s1= str.toLowerCase();
        String s2= "where ";
        int indexOfStart= s1.indexOf(s2);
        if(indexOfStart!=0)
        {
            return Conditions;
        }
        int indexEndCondition = s1.indexOf(" group ");
        if(indexEndCondition==-1)
        {
            indexEndCondition = s1.indexOf(" having ");
        }
        
        if(indexEndCondition==-1)
        {
            str = str.substring(indexOfStart + s2.length(),str.length()).trim();
        }
        else
        {   
            str = str.substring(indexOfStart + s2.length(),indexEndCondition).trim();
        }
        String[] Relation={" and "," or "};
      
        while(str.length()!=0)
        {
           int i;
           int indexOfRelation = -1; 
           String strLower= str.toLowerCase();
           for(i=0;i< Relation.length;i++)
           {
               indexOfRelation = strLower.indexOf(Relation[i]);
               if(indexOfRelation!=-1)
               {
                   break;
               }
           }
           
           MyExpression express;
           if(indexOfRelation!=-1)
           {
               String temp =str.substring(0,indexOfRelation);              
               express= new MyExpression(temp);
               express.Relationship= Relation[i];
               str=str.substring(indexOfRelation + Relation[i].length(), str.length()).trim();
           }
           else
           {
               express= new MyExpression(str.trim());
               express.Relationship="";
               str="";
           }
           
           Conditions.add(express);       
        }
        
        return Conditions;
    }
    
    public static boolean isCharacter(String str)
    {
        try{	
                       //You can try converting the value to a double, so that you can know if it is a numeric value.
                int i = Integer.parseInt(str);
                return false;
        }catch(NumberFormatException number)
        {
                return true;
        }
    }
    
    public static boolean isNumber(String str)
    {
        if(str.toLowerCase().compareTo(str.toUpperCase())==0)
        {
            return true;
        }
        return false;
    }
    
    public static boolean isContainJoin(String str)
    {
        String temp = str.toLowerCase();
        if(temp.contains(" join "))
        {
            return true;
        }
        return false;
    }
}
